/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.print;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.UIManager;

import net.abbp.core.utils.UnitUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.reportgear.report.setting.ReportSettings;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * 预览窗体内部视图
 * 
 * @version 1.0 2011-3-23
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class InnerPreviewPane extends JPanel implements Scrollable {
	private static final Logger logger = LoggerFactory.getLogger(InnerPreviewPane.class.getName());
	private ReportPage[] reportPages;

	private int columnIndex = -1;

	public InnerPreviewPane() {
	}

	/**
	 * 打印报表页面
	 * 
	 * @param reportPage
	 */
	public void print(ReportPage reportPage) {
		if (reportPage == null) {
			print(new ReportPage[0]);
		} else {
			print(new ReportPage[] { reportPage });
		}
	}

	public void print(ReportPage[] reportPages) {
		this.reportPages = reportPages;

		getParent().validate();
		getParent().doLayout();

		repaint(10L);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		if ((this.reportPages == null) || (this.reportPages.length <= 0)) {
			return;
		}

		Graphics2D g2 = (Graphics2D) g;
		Dimension dim = getSize();
		ReportPage reportPage = this.reportPages[0];

		int pageWidth = UnitUtils.inch2pixel(reportPage.getPaperWidth());
		int pageHeight = UnitUtils.inch2pixel(reportPage.getPaperHeight());

		g2.translate((dim.getWidth() - pageWidth) / 2.0D, 4.0D);
		reportPage.paint(g2);
		g2.setPaint(UIManager.getColor("controlDkShadow"));
		GraphHelper.drawRect(g2, 0.0D, 0.0D, pageWidth, pageHeight);
	}

	@Override
	public Dimension getPreferredSize() {
		if ((this.reportPages == null) || (this.reportPages.length <= 0)) {
			return new Dimension(UnitUtils.inch2pixel(PaperSize.PAPERSIZE_A4.getWidth()), UnitUtils
					.inch2pixel(PaperSize.PAPERSIZE_A4.getHeight()));
		}

		return new Dimension(UnitUtils.inch2pixel(this.reportPages[0].getPaperWidth()) + 8, UnitUtils
				.inch2pixel(this.reportPages[0].getPaperHeight()) + 8);
	}

	public Dimension getPreferredScrollableViewportSize() {
		return getPreferredSize();
	}

	public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
		switch (orientation) {
		case PageFormat.PORTRAIT:
			return (visibleRect.height / 10);
		case PageFormat.LANDSCAPE:
			return (visibleRect.width / 10);
		}
		throw new IllegalArgumentException("无效的打印方向 : " + orientation);
	}

	public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
		switch (orientation) {
		case PageFormat.PORTRAIT:
			return visibleRect.height;
		case PageFormat.LANDSCAPE:
			return visibleRect.width;
		}
		throw new IllegalArgumentException("无效的打印方向 : " + orientation);
	}

	public boolean getScrollableTracksViewportWidth() {
		return ((getParent() instanceof JViewport) && (getParent().getWidth() > getPreferredSize().width));
	}

	public boolean getScrollableTracksViewportHeight() {
		return ((getParent() instanceof JViewport) && (getParent().getHeight() > getPreferredSize().height));
	}
}
